package com.kms.derby;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class Connections {
	
	public static Connection conn = null;
	
	public static Connection getConnection(HttpServletRequest request) throws SQLException{
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		JdbcTemplate jdbctemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
		DataSource ds = jdbctemplate.getDataSource();
		conn = ds.getConnection();
		return conn;
	}
	
	public static Object getServiceBean(HttpServletRequest request,String servicename){
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		return context.getBean(servicename);
	}
}
